package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{



    public void work(BookList bookList) {
        System.out.println("Del Book");

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Book ID：");

        String name = sc.nextLine();
        int currentSize = bookList.getUsedSize();

        int pos = -1;
        int i = 0;
        for (; i < currentSize; i++) {
            if (bookList.getBook(i).getName().equals(name)) {
                pos = i;
                break;
            }
        }

        if(i == currentSize){
            System.out.println("NO SUCH BOOK");
            return;
        }


        for(int j = pos; j < currentSize-1; j++){
            Book book = bookList.getBook(j+1);
            bookList.setBook(j, book);
        }

        bookList.setBook(currentSize-1, null);

        bookList.setUsedSize(currentSize-1);
        System.out.println("Book deleted successfully");
    }
}
